package com.utonw.carloan.fragment.applyloan;

import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.sensetime.library.finance.ocr.card.IdCardInfo;
import com.sensetime.library.finance.ocr.card.NativeIdCardSide;
import com.sensetime.sample.a.IdCardActivity;
import com.utonw.carloan.R;
import com.utonw.carloan.fragment.BaseFragment;
import com.utonw.carloan.function.GlobalLoanFunction;
import com.utonw.carloan.global.Constants;
import com.utonw.carloan.utils.ChooseBarUtils;
import com.utonw.carloan.utils.UIUtils;

import java.util.ArrayList;

/**
 * Created by SEELE on 2017/3/22.
 */

/**
 * 申请贷款流程1：个人信息
 */
public class LoanStepOneFragment extends BaseFragment {
    //上传并识别身份证正面
    private ImageView mUploadIdcardFront;
    //上传并识别身份证背面
    private ImageView mUploadIdcardBack;

    //扫描个人信息
    private EditText mName;//姓名
    private EditText mIdCard;//身份证号
    private RadioButton mMale,mFemale;//男女
    private TextView mBirthDay;//出生日期
    private TextView mLocal;//户籍

    @Override
    public int setLayout() {
        return R.layout.fragment_applyloan_step_one;
    }

    @Override
    public void initView(View view) {
        mUploadIdcardFront = (ImageView) mActivity.findViewById(R.id.iv_btn_upload_idcard_front);
        mUploadIdcardBack = (ImageView) mActivity.findViewById(R.id.iv_btn_upload_idcard_back);
    }

    @Override
    public void initData() {
        //初始化PickerView选择器
        final GlobalLoanFunction globalLoanFunction = new GlobalLoanFunction();
        //设置ChooseBar标题
        ChooseBarUtils titleUtils = new ChooseBarUtils(getHoldingActivity()) {
            @Override
            protected void setChooseBarClickListener(int view) {

            }

            @Override
            protected void setChooseBarClickListener(int view, TextView toShow) {
                switch (view) {
                    case R.id.chushengriqi:

                        globalLoanFunction.setTempData(null, toShow).timePickerView(getContext());

                        break;

                    case R.id.hujidizhi:


                        globalLoanFunction.setTempData(null, toShow).localPickerView(getContext());

                        break;

                    case R.id.xueli:

                        final ArrayList<String> data1 = new ArrayList<>();
                        data1.add("本科");
                        data1.add("大专");
                        data1.add("博士及以上");
                        data1.add("硕士");
                        data1.add("高中及中专");
                        data1.add("初中及以下");

                        globalLoanFunction.setTempData(data1, toShow).oriPickerView(getContext());

                        break;

                    case R.id.hunyinzhuangkuang:

                        final ArrayList<String> data2 = new ArrayList<>();
                        data2.add("未婚");
                        data2.add("已婚");
                        data2.add("离异");
                        data2.add("丧偶");
                        data2.add("再婚");

                        globalLoanFunction.setTempData(data2, toShow).oriPickerView(getContext());

                        break;

                    case R.id.hunyinnianxian:

                        final ArrayList<String> data3 = new ArrayList<>();
                        for (int i = 1; i < 100; i++) {
                            data3.add(i+"年");
                        }

                        globalLoanFunction.setTempData(data3, toShow).oriPickerView(getContext());

                        break;

                    case R.id.juzhuqingkuang:

                        final ArrayList<String> data4 = new ArrayList<>();
                        data4.add("父母");
                        data4.add("子女");
                        data4.add("配偶");
                        data4.add("其他");

                        globalLoanFunction.setTempData(data4, toShow).oriPickerView(getContext());

                        break;

                    case R.id.xianzhuzhaileixing:

                        final ArrayList<String> data5 = new ArrayList<>();
                        data5.add("自建");
                        data5.add("自购");
                        data5.add("亲属住房");
                        data5.add("单位住房");
                        data5.add("租用");

                        globalLoanFunction.setTempData(data5, toShow).oriPickerView(getContext());

                        break;


                }
            }

            @Override
            protected void getCheckedViewListener(int includeId, int sTitle, View toCheck) {
                setCheckViews(sTitle,toCheck);
                switch (includeId){
                    case R.id.xingming:
                        mName= (EditText) toCheck;
                        break;
                    case R.id.shenfenzhenghao:
                        mIdCard= (EditText) toCheck;
                        break;

                    case R.id.chushengriqi:
                        mBirthDay= (TextView) toCheck;
                        break;

                    case R.id.hujidizhi:
                        mLocal= (TextView) toCheck;
                        break;
                }

            }

            @Override
            public void onToggleButtonListener(int buttonView, boolean isChecked) {

            }

            @Override
            public void getEditTextWithTipsListener(int includeId, EditText tempEt1) {

            }

            @Override
            public void onRadioButtonListener(RadioButton rMale, RadioButton rFemale) {
                mMale=rMale;
                mFemale=rFemale;
            }

            @Override
            public void onImageChooseBarListener(int view, ImageView mIndicator, TextView mInstruction, int sTitle) {

            }

            @Override
            public void getChangeWordsEditTextListener(int includeId, EditText tempEt1, int sTitle) {

            }

            @Override
            public void setVerificationListener() {

            }
        };

        titleUtils.initChooseBar("0","0","",-1,R.id.shenfenzhengshibie, R.string.apply_page_one_lookup_id);
        titleUtils.initChooseBar("1","1","customerName",R.id.xingming, R.string.apply_page_one_name, ChooseBarUtils.ResultState.CHANGE_WORDS, "请输入姓名");
        titleUtils.initChooseBar("3","1","idCard",R.id.shenfenzhenghao, R.string.apply_page_one_id, ChooseBarUtils.ResultState.CHANGE_WORDS,"请输入身份证号");
        titleUtils.initChooseBar("0","1","sex",R.id.xingbie, R.string.apply_page_one_sex, ChooseBarUtils.ResultState.RADIO_BUTTON, null);
        titleUtils.initChooseBar("0","1","birthDate",R.id.chushengriqi, R.string.apply_page_one_birthday);
        titleUtils.initChooseBar("0","1","kosekiAddress",R.id.hujidizhi, R.string.apply_page_one_my_location);
        titleUtils.initChooseBar("0","0","homeTel",R.id.zhuzhaidianhua, R.string.apply_page_one_tel, ChooseBarUtils.ResultState.CHANGE_WORDS, "选填");
        titleUtils.initChooseBar("0","1","education",R.id.xueli, R.string.apply_page_one_degrees);
        titleUtils.initChooseBar("0","1","maritalStatus",R.id.hunyinzhuangkuang, R.string.apply_page_one_marriage_state);
        titleUtils.initChooseBar("0","0","marryPeriod",R.id.hunyinnianxian, R.string.apply_page_one_marriage_time);
        titleUtils.initChooseBar("0","1","residencePeriod",R.id.bendijuzhushijian, R.string.apply_page_one_local_time, ChooseBarUtils.ResultState.EDIT_TEXT_WITH_TIPS,"年");
        titleUtils.initChooseBar("0","1","numberOfChildren",R.id.zinvgeshu, R.string.apply_page_one_sons, ChooseBarUtils.ResultState.EDIT_TEXT_WITH_TIPS,"个");
        titleUtils.initChooseBar("0","1","cohabitant",R.id.juzhuqingkuang, R.string.apply_page_one_local_state);
        titleUtils.initChooseBar("8","1","liveAddress",R.id.xianjiatingzhuzhi, R.string.apply_page_one_location, ChooseBarUtils.ResultState.CHANGE_WORDS, "请输入详细地址");
        titleUtils.initChooseBar("0","1","buildingType",R.id.xianzhuzhaileixing, R.string.apply_page_one_location_type);
        titleUtils.initChooseBar("0","0","roomCharge",R.id.yuezu, R.string.apply_page_one_month_pay, ChooseBarUtils.ResultState.CHANGE_WORDS, "请填写月租");
        titleUtils.initChooseBar("0","1","residencePeriodCurrent",R.id.xiandizhijuzhushijian, R.string.apply_page_one_location_till_now, ChooseBarUtils.ResultState.EDIT_TEXT_WITH_TIPS,"年");

        //绑定上传身份证正面监听
        mUploadIdcardFront.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), IdCardActivity.class);
                intent.putExtra(IdCardActivity.EXTRA_CARD_SIDE, NativeIdCardSide.CV_FINANCE_IDCARD_SIDE_FRONT);
                getFragment().startActivityForResult(intent, Constants.REQUEST_CODE_SCAN);

            }
        });

        //绑定上传身份证背面监听
        mUploadIdcardBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), IdCardActivity.class);
                intent.putExtra(IdCardActivity.EXTRA_CARD_SIDE, NativeIdCardSide.CV_FINANCE_IDCARD_SIDE_BACK);
                getFragment().startActivityForResult(intent, Constants.REQUEST_CODE_SCAN);
            }
        });
    }

    @Override
    protected void getIdcardFrontInfo(IdCardInfo idCardInfo) {
        if (null!=idCardInfo) {
            int[] colors = idCardInfo.getFrontImage();
            if(colors != null && colors.length > 0) {
                Bitmap cardImage = Bitmap.createBitmap(colors,
                        IdCardInfo.IMAGE_WIDTH,
                        IdCardInfo.IMAGE_HEIGHT,
                        Bitmap.Config.ARGB_8888);
                mUploadIdcardFront.setImageBitmap(cardImage);
            }
            mName.setText(idCardInfo.getName());
            switch (idCardInfo.getSex()){
                case "男":
                    mMale.setChecked(true);
                    break;
                case "女":
                    mFemale.setChecked(true);
                    break;
            }
            mBirthDay.setText(idCardInfo.getBirthday());
            mBirthDay.setTextColor(UIUtils.getColor(R.color.fontColor));
            mLocal.setText(idCardInfo.getAddress());
            mLocal.setTextColor(UIUtils.getColor(R.color.fontColor));
            mIdCard.setText(idCardInfo.getNumber());

        }
    }

    @Override
    protected void getIdcardBackInfo(IdCardInfo idCardInfo) {
        if (idCardInfo != null) {
            int[] colors = idCardInfo.getBackImage();
            if(colors != null && colors.length > 0) {
                Bitmap cardImage = Bitmap.createBitmap(colors,
                        IdCardInfo.IMAGE_WIDTH,
                        IdCardInfo.IMAGE_HEIGHT,
                        Bitmap.Config.ARGB_8888);
                mUploadIdcardBack.setImageBitmap(cardImage);
            }
        }
    }

    @Override
    protected void getBankcardInfo(Intent cardData) {

    }

    @Override
    protected void getCreditcardInfo(Intent cardData) {

    }
}
